import { defineConfig } from 'vite'
import path from 'path'
import reactRefresh from '@vitejs/plugin-react-refresh'
import WindsCSS from 'vite-plugin-windicss'
export default defineConfig({
  plugins: [
    reactRefresh(),
    WindsCSS(),
  ],
  css: {
    preprocessorOptions: {
      scss:{
        javascriptEnabled: true,

      }
    },
  },
  //暴露环境变量
  define: {
    'process.env': {}
  },
  server: setServerProxy(),
  resolve: setResolve(),
  build: setBuild(),
})

// 静态资源合并分类打包
function setBuild() {
  return {
    assetsDir: 'static/images/',
    brotliSize: false, // 关闭计算包大小
    rollupOptions: {
      output: {
        chunkFileNames: 'static/js/[name]-[hash].js',
        entryFileNames: 'static/js/[name]-[hash].js',
        assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
      },
    },
  }
}

function setResolve() {
  return {
    alias: {
      '~': path.resolve(__dirname, './'),
      '@': path.resolve(__dirname, 'src'),
      '/images': 'src/assets/images', // 图片使用 <img src="/images/logo.svg" />
    },
  }
}

function setServerProxy() {
  return {
    port: 8081, // 开发环境启动的端口
    proxy: {
      '/mobilea': {
        target: 'http://localhost:8210', // 当遇到 /api 路径时，将其转换成 target 的值
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  }
}